package cn.b.sky.mes.mm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.b.sky.mes.mm.dao.DistributionDao;
import cn.b.sky.mes.mm.dao.RequirementDao;
import cn.b.sky.mes.mm.enums.RequirementStatus;
import cn.b.sky.mes.mm.model.Distribution;
import cn.b.sky.mes.mm.model.Requirement;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.condition.DistributionCondition;
import cn.b.sky.mes.mm.model.condition.RequirementCondition;
import cn.b.sky.mes.mm.service.RequirementService;
import cn.b.sky.common.model.Page;
import cn.b.sky.user.service.model.ECode;

/**
 * 
 * <strong>Title : RequirementServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.service.impl<br></strong>
 * <strong>Description : </strong>物料需求数据服务实现类<br> 
 * <strong>Create on : 2016年9月13日 上午11:31:48<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("requirementService")
public class RequirementServiceImpl implements RequirementService {

  @Autowired
  private RequirementDao requirementDao;

  @Autowired
  private DistributionDao distributionDao;

  @Override
  public void batchSaveRequirements(List<Requirement> requirements) {
    for (Requirement requirement : requirements) {
      saveRequirement(requirement);
    }
  }

  private void saveRequirement(Requirement requirement) {
    requirementDao.insertRequirement(requirement);
  }

  @Override
  public void insertRequirement(Requirement requirement) {
    requirementDao.insertRequirement(requirement);

  }

  @Override
  public double getHistoryRequirementCount(Requirement requirement) {
    return requirementDao.getHistoryRequirementCount(requirement);
  }

@Override
public List<Requirement> SelectRequirementListByCondition(
		RequirementCondition condition) {
	    if(condition==null){
	    	return null;
	    }
	return requirementDao.selectRequirementListByCondition(condition);
}

@Override
public int SelectRequirementCountByCondition(RequirementCondition condition) {
	  if(null==condition){
		  return 0;
	  }
	return requirementDao.selectRequirementCountByCondition(condition);
}

  @Override
  public Map<String, Integer> getRequirementStatus(RequirementCondition condition, List<ECode> eCodes) {
    HashMap<String, Integer> requirementStatusMap = new HashMap<String, Integer>();
    int totalCount = 0;
    String[] statuseCodes = new String[eCodes.size()];
    for (int i = 0; i < eCodes.size(); i++) {
      statuseCodes[i] = eCodes.get(i).geteCode();
    }
    condition.setStatusECodes(statuseCodes);

    Set<Map<String, Object>> set = requirementDao.getRequirementsCount(condition);
    for (Map<String, Object> map : set) {
      Integer count = Integer.valueOf(map.get("COUNT").toString());
      requirementStatusMap.put(map.get("STATUSECODE").toString(), count);
      totalCount += count;
    }

    condition
        .setStatusECodes(new String[] { RequirementStatus.CREATED.getStatus(), RequirementStatus.CLOSED.getStatus() });
    requirementStatusMap.put("overtimeCount", requirementDao.getOvertimeRequirementCount(condition));
    requirementStatusMap.put("totalCount", totalCount);
    condition.setStatusECodes(new String[] {});
    condition.setDistributionTimeoutFlag(null);
    return requirementStatusMap;
  }

  @Override
  public Page getRequirementsByCondition(RequirementCondition condition) {
    condition.setData(requirementDao.getRequirementsByCondition(condition));
    condition.setTotalCount(requirementDao.getRequirementCountByCondition(condition));
    return condition;
  }

  @Override
  public Page getOvertimeRequirementsByCondition(RequirementCondition condition) {
    condition
        .setStatusECodes(new String[] { RequirementStatus.CREATED.getStatus(), RequirementStatus.CLOSED.getStatus() });
    condition.setData(requirementDao.getOvertimeRequirementsByCondition(condition));
    condition.setTotalCount(requirementDao.getOvertimeRequirementCount(condition));
    return condition;
  }

  @Override
  public List<Distribution> getDistributionsByRequirement(String siteCode, String requirementNum) {
    return distributionDao.getDistributionsByRequirementNum(siteCode, requirementNum);
  }

  @Override
  public Page getDistributionsByRequirement(DistributionCondition condition) {
    condition.setData(distributionDao.getDistributionsByRequirement(condition));
    condition.setTotalCount(distributionDao.getDistributionCountByRequirement(condition));
    return condition;
  }

@Override
public Requirement getRequirement(String siteCode, String requirementNum) {
	if(StringUtils.isBlank(siteCode) || StringUtils.isEmpty(requirementNum)){
		return null;
	}
	return requirementDao.getRequirement(siteCode,requirementNum);
}

@Override
public List<Requirement> selectRequirement(String siteCode,
		String requirementNum) {
	if (!StringUtils.isBlank(siteCode) && !StringUtils.isBlank(requirementNum)) {
	      return requirementDao.selectRequirement(siteCode, requirementNum);
	    }
	return null;
}

@Override
public String cancelStatusECode(String siteCode, String code) {
	 String message = null;    
	 Requirement requirement=requirementDao.getRequirement(siteCode, code);	    
	    if (requirement.getStatusECode().equals(RequirementStatus.CANCELED.getStatus())) {
	      message="StatusIsCanceled.M";
	    } else {
	    	requirement.setStatusECode(RequirementStatus.CANCELED.getStatus());
	    	requirementDao.updateCancelStatusECode(requirement);
	    }   
	    return message;
}
}